package com.huohuo.concurrent.syn;

import com.huohuo.concurrent.simple.OrderNumberGenerator;

import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;

public class OrderSynService implements Runnable{

//    @Autowired
//    OrderNumberGenerator orderNumberGenerator;

    private OrderNumberGeneratorSyn orderNumberGeneratorSyn = new OrderNumberGeneratorSyn();

    //并发包
    private static CountDownLatch countDownLatch = new CountDownLatch( 50 );

    private static List<String> result = new Vector<>(  );

    /**
     * 使用多线程模拟很多的请求
     */
    @Override
    public void run() {
        countDownLatch.countDown();//递减锁存器的计数，如果计数到达零，则释放所有等待的线程。
        result.add(orderNumberGeneratorSyn.generatorNumber2());
        //System.out.println("run方法执行结束");//这后面不能写代码了

    }

    /**
     * 方法的测试
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        for (int i=0;i<50;i++){
            new Thread( new OrderSynService()).start();
        }
        countDownLatch.await();// 使当前线程在锁存器倒计数至零之前一直等待
        Thread.sleep( 1000 );
        Collections.sort( result );
        for (String str:result){
            System.out.println("订单号："+str);
        }
    }




}
